package leetcode_top;
import org.junit.*;
public class Ex122 {
    class Solution {

        /* 
            0: 不持有股票
            1： 持有股票
        */
        public int maxProfit(int[] prices) {
            int len;
            if (prices == null || (len = prices.length) == 0) return 0;
            int[][] f = new int[len][2];
            f[0][0] = 0; //第一天不买
            f[0][1] = -prices[0]; //买了
            for (int i = 1; i < len; i++) {
                f[i][0] = Math.max(f[i - 1][1] + prices[i], f[i - 1][0]);
                f[i][1] = Math.max(f[i - 1][1], f[i - 1][0] - prices[i]);
            }
            return f[len - 1][0];
        }


        public int maxProfit2(int[] prices) {
            int sum = 0, len;
            if (prices == null || (len = prices.length) == 0) return 0;
            for (int i = 1; i < len; i++) {
                if (prices[i] > prices[i - 1]) 
                    sum += (prices[i] - prices[i - 1]);
            }
            return sum;
        }
    }

    @Test
    public void test() {
        Solution s = new Solution();
        int[] nums = new int[]{1,2,3,4,5};//7,1,5,3,6,4
        System.out.println(s.maxProfit2(nums));
    }
}
